<template>
  <div class="history-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>测试历史记录</h2>
          <el-button type="danger" @click="handleClearHistory" :disabled="!testStore.testHistory.length">
            清空历史
          </el-button>
        </div>
      </template>
      
      <el-empty v-if="!testStore.testHistory.length" description="暂无测试记录" />
      
      <el-table v-else :data="testStore.testHistory" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="url" label="测试URL" show-overflow-tooltip />
        <el-table-column prop="startTime" label="开始时间" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.startTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="duration" label="测试时长" width="120">
          <template #default="scope">
            {{ scope.row.duration }}秒
          </template>
        </el-table-column>
        <el-table-column prop="concurrentUsers" label="并发用户数" width="120" />
        <el-table-column prop="totalRequests" label="总请求数" width="120" />
        <el-table-column prop="successRate" label="成功率" width="120">
          <template #default="scope">
            <el-progress 
              :percentage="scope.row.successRate" 
              :status="scope.row.successRate >= 90 ? 'success' : 'exception'"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="scope">
            <el-button 
              type="primary" 
              link
              @click="handleViewDetail(scope.row.id)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useTestStore } from '../stores/test'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const testStore = useTestStore()

const formatDate = (date) => {
  return new Date(date).toLocaleString()
}

const handleViewDetail = (id) => {
  router.push(`/detail/${id}`)
}

const handleClearHistory = async () => {
  try {
    await ElMessageBox.confirm(
      '确定要清空所有测试历史记录吗？此操作不可恢复！',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    testStore.clearHistory()
  } catch {
    // 用户取消操作
  }
}
</script>

<style scoped>
.history-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-table {
  margin-top: 20px;
}
</style> 